# the c++ compilation rules
SHELL=/bin/bash

CC = g++
INCLUDES = -I../
CFLAGS = -O -c
LDFLAGS = -framework OpenGL -framework GLUT -lglew

# the MPIX compilation paths for java execution
CLASSPATH = /Applications/Eclipse/plugins/org.antlr_2.7.6/antlr.jar:../bin

# c++ matpix library files
MPX_SRC = ../MatPix/util.cpp ../MatPix/PBuffer.cpp
MPX_OBJS = ${MPX_SRC:%.cpp=%.o}

# Successful and runtime-error tests go here
TESTS = badDim_dotprod_fail_r.mpx range_inv_row_fail_r.mpx range_inv_col_fail_r.mpx range_oob_col_fail_r.mpx range_oob_row_fail_r.mpx range_neg_fail_r.mpx badDim_add_fail_r.mpx recursive.mpx matDecl.mpx daxpy.mpx dotprod.mpx logictest.mpx functiondef.mpx ctrlFlow.mpx slicetest.mpx sliceAssign.mpx transpose.mpx gauss_jordan.mpx arithmetic.mpx constmatrix.mpx regression.mpx sum.mpx 

# mpx failures (scoping, etc) go here
TESTS_MPX_FAIL = constmatrix_notScalar_fail_m.mpx matIndex_fail_m.mpx syntax_fail_m.mpx constmatrix_fail_m.mpx funcDef_dup_fail_m.mpx funcCall_notDef_fail_m.mpx scope_func1_fail_m.mpx scope_func2_fail_m.mpx lvalue_fail_m.mpx scope_if_fail_m.mpx
TEST_MPX_FAIL_CPP = ${TESTS_MPX_FAIL:%.mpx=cpp/%.cpp}

# the intermediate files follow from substitution rules
TEST_CPP = ${TESTS:%.mpx=cpp/%.cpp}
TEST_OBJS = ${TESTS:%.mpx=cpp/%.o}
TEST_EXEC = ${TESTS:%.mpx=%}
TEST_EXEC2 = ${TESTS:%.mpx=bin/%}
TEST_FILES = ${TEST_CPP} ${TEST_OBJS} ${TEST_EXEC2}

# the log files for each compilation step and running the tests
LOG_TEST_CPP = ${TESTS:%.mpx=cpp/log/%.cpp.log}
LOG_TEST_OBJS = ${TESTS:%.mpx=cpp/log/%.o.log}
LOG_TEST_EXEC = ${TESTS:%.mpx=bin/log/%.log}
LOG_TEST_FILES = ${LOG_TEST_CPP} ${LOG_TEST_OBJS} ${LOG_TEST_EXEC}

# this line says not to delete the cpp files
# which are "intermediate" files
.SECONDARY:  ${TEST_CPP}

# the main make rule - i.e. what happens if you just type "make"
all: ${TEST_EXEC} test_fail

# the rule to create .cpp files from .mpx files
# note - the rule assumes the input files are in the current dir
# and the output files go in the cpp/ directory
cpp/%.cpp: mpx/%.mpx
	@echo "trying to MPIX compile " $@ " from " $<
	java -cp ${CLASSPATH} Main $< $@ $(HAS_GPU) 2> $<.log
	mv $<.log mpx/log/

# the rule to create .o files from .cpp files
# note - the rule assumes the input and output files are in the same dir
%.o: %.cpp
	@echo "trying to create object file " $@ " from " $<
	g++ ${INCLUDES} ${CFLAGS} -o $@ $< 2> $<.log
	mv $<.log cpp/log/

# the rule to create the executables
# note - the rule assumes the input files are in the cpp/ dir
# and the output files go in the bin/ directory
${TEST_EXEC} : ${TEST_OBJS} ${MPX_OBJS}
	@echo "trying to create executable " $@ " from " $@.o
	g++ -o bin/$@ ${LDFLAGS} cpp/$@.o ${MPX_OBJS}

clean:
	rm -f ${TEST_FILES} ${LOG_TEST_FILES} ${MPX_OBJS}
	@echo "all cleaned up!"

# this will run all the tests
# print out the log info
test_fail:  ${TEST_MPX_FAIL_CPP}
test : ${TEST_EXEC} test_fail
#	@echo "running executable " $@
#	bin/$@ > bin/log/$@.log
#	${foreach tests, ${TEST_EXEC}, ${shell echo "bin/${tests} > bin/log/${tests}.log" } }	
#	${foreach tests, ${TEST_EXEC}, ${shell bin/${tests} > bin/log/${tests}.log } }
	bin/run.py
